package com.winchain.chainbill.bill.biz.discount.rx;

import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillTranEvent;
import com.winchain.chainbill.bill.biz.discount.service.BillDctEventService;
import com.winchain.chainbill.bill.biz.send.rx.BatchBillEvent;
import com.winchain.chainbill.esign.service.BatchSignService;
import com.winchain.chainbill.esign.vo.BatchSignVo;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillSerFeeBatchSignSubscriber extends OnErrorNotImplementedSubscriber<BatchBillEvent<String, CbDct>> {
	@Autowired
	private BillDctEventService billDctEventService;
	@Autowired
	private BatchSignService batchSignService;
	private static final String REASON = "����������������";

	public void onApplicationEvent(BatchBillEvent<String, CbDct> t)
  {
    String phoneCode = t.getStringValue("phoneCode");
    String location = t.getStringValue("location");
    String sealCode = t.getStringValue("sealCode");
    
    List<String> signBillIds = new ArrayList();
    String signBillId = null;
    List<String> records = t.getRecs();
    for (String record : records)
    {
      signBillId = record;
      BillTranEvent<CbDct> event = this.billDctEventService.getBillTranEvent(record, false);
      
      List<Bill<CbDct>> children = (List<Bill<CbDct>>) event.getChildren();
      if ((children != null) && (children.size() > 0))
      {
        List<Bill<CbDct>> bills = children;
        for (Iterator localIterator2 = bills.iterator(); localIterator2.hasNext();)
        {
          Bill<CbDct> dctBill = (Bill)localIterator2.next();
          CbDct dct = (CbDct)dctBill.getSource();
          if ((dct != null) && (dct.getDctId() != null))
          {
            if (!dct.getBuyerId().equals(dct.getSellerId())) {
              break;
            }
            signBillId = dct.getBillId();
          }
        }
      }
      signBillIds.add(signBillId);
    }
    if (signBillIds.size() == 0) {
      return;
    }
    BatchSignVo batchSignVo = new BatchSignVo();
    batchSignVo.setSignBillIds(signBillIds);
    batchSignVo.setBizType("51");
    batchSignVo.setPhoneCode(phoneCode);
    batchSignVo.setLocation(location);
    batchSignVo.setReason("����������������");
    batchSignVo.setSealCode(sealCode);
    this.batchSignService.batchSign(batchSignVo);
  }
}
